package com.szqhyxj.youxiangju.biz.partnerHome.equipment.subordinateList;

import com.szqhyxj.youxiangju.BaseConsumer;
import com.szqhyxj.youxiangju.biz.partnerHome.equipment.PartnerEquipmentModel;
import com.szqhyxj.youxiangju.biz.partnerHome.equipment.bean.SubordinateInfo;
import com.szqhyxj.youxiangju.sys.AbBasePresenter;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * XiaoPanYu
 * on 2018/11/22
 */
public class SubordinateInfoPresenter extends AbBasePresenter implements SubordinateInfoContract.Presenter{

    private SubordinateInfoContract.View view;
    public SubordinateInfoPresenter(SubordinateInfoContract.View view){
        this.view= view;
    }


    @Override
    public void init(){
        addDisposables(PartnerEquipmentModel.outOrgan("").observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseConsumer<List<SubordinateInfo>>(){
                    @Override
                    protected void error(String code, String msg){
                        view.dismissLoading();
                        view.showToast(msg);
                    }

                    @Override
                    protected void successAccept(List<SubordinateInfo> subordinateInfoList){
                        view.dismissLoading();
                        if(subordinateInfoList.size()>0){
                            view.showInfoList(subordinateInfoList);
                        }else {
                            view.showNone();
                        }
                    }
                }, new Consumer<Throwable>(){
                    @Override
                    public void accept(Throwable throwable) throws Exception{
                        view.dismissLoading();
                        view.showToast(throwable.getMessage());
                    }
                }));
    }

    @Override
    public void searchInfo(String name){
        addDisposables(PartnerEquipmentModel.outOrgan(name).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseConsumer<List<SubordinateInfo>>(){
                    @Override
                    protected void error(String code, String msg){
                        view.dismissLoading();
                        view.showToast(msg);
                    }

                    @Override
                    protected void successAccept(List<SubordinateInfo> subordinateInfoList){
                        view.dismissLoading();
                        if(subordinateInfoList.size()>0){
                            view.showInfoList(subordinateInfoList);
                        }else {
                            view.showNone();
                        }
                    }
                }, new Consumer<Throwable>(){
                    @Override
                    public void accept(Throwable throwable) throws Exception{
                        view.dismissLoading();
                        view.showToast(throwable.getMessage());
                    }
                }));
    }

    @Override
    public void confirmOut(String devNo, String toOrganId){
        addDisposables(PartnerEquipmentModel.confirmOut(devNo,Integer.parseInt(toOrganId)).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseConsumer<String>(){
                    @Override
                    protected void error(String code, String msg){
                        view.dismissLoading();
                    }

                    @Override
                    protected void successAccept(String o){
                        view.dismissLoading();
                        view.navSucceed();
                    }
                }, new Consumer<Throwable>(){
                    @Override
                    public void accept(Throwable throwable) throws Exception{
                        view.dismissLoading();
                    }
                }));
    }

    @Override
    public void addDisposables(Disposable disposable){
        super.addDisposables(disposable);
        view.showLoading();
    }
}
